using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Deepcode.Flux.Core.Systems.CMS;

public partial class Private_CMS_main_Browser : Deepcode.Flux.Core.UI.AdministrationModulePage
{
	#region Permission Code Override
	public override string RequiredPermissionCode { get { return ""; } }
	#endregion
	#region Page Load Event
	/// <summary>
	/// Page Load event
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
		{
			// Populate the tree for on-demand loading
			BindTreeData( _tree.Nodes, -1 );
		}
	}
	#endregion
	#region Initialisation
	/// <summary>
	/// Page initialisation
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Init(object sender, EventArgs e)
	{
		_tree.TreeNodePopulate += new TreeNodeEventHandler(_tree_TreeNodePopulate);
		_tree.TreeNodeCollapsed += new TreeNodeEventHandler(_tree_TreeNodeCollapsed);
		_tree.TreeNodeExpanded += new TreeNodeEventHandler(_tree_TreeNodeExpanded);
	}
	#endregion
	#region Tree Databinding
	#region User view mode interrogation

	protected bool myModeSet = false;
	protected ViewContentItemMode myMode = ViewContentItemMode.TitleOnly;

	protected ViewContentItemMode MyMode
	{
		get
		{
			if (!myModeSet)
			{
				myMode = (ViewContentItemMode) UserSetting("/CMS", "Mode", (int) ViewContentItemMode.TitleOnly);
				myModeSet = true;
			}

			return myMode;
		}
	}

	#endregion
	/// <summary>
	/// Loads the data from the distro tree and devices into the tree
	/// </summary>
	protected void BindTreeData(TreeNodeCollection tnc, int ParentID)
	{
		ContentManager cms = new ContentManager();
		ContentObject [] items = cms.AdxGetByParentID( ParentID, ContentObjectSortOrder.RANK, Deepcode.Flux.Core.Data.DataSortDirection.ASCENDING);

		// Add nodes
		foreach (ContentObject i in items )
		{
			TreeNode node = new TreeNode();
			node.Text = i.NodeTitle;
			if( MyMode == ViewContentItemMode.NameOnly ) node.Text = i.NodeName;
			if( MyMode == ViewContentItemMode.TitleAndName ) node.Text = i.NodeTitle + " (" + i.NodeName + ")";
			node.Value = i.PK_ID.ToString();
			node.ImageUrl = "~/admin/private/cms/main/img/folderclosed.gif";

			node.NavigateUrl = 
				String.Format("javascript:ReturnNode('{0}', '{1}', '{2}{3}.aspx');",
					i.PK_ID,
					i.NodeTitle.Replace("'", "\\'"),
					i.NodePath.Replace("'", "\\'"), 
					i.NodeName.Replace("'", "\\'"));

			// See if there are any sub directories
			if( i.ChildrenStaging.Length < 1 )
			{
				node.PopulateOnDemand = false;
				node.Expanded = false;
			}
			else
			{
				node.PopulateOnDemand = true;

				bool isExpand = IsExpanded(i.PK_ID);
				node.Expanded = isExpand;
				if (isExpand)
					node.ImageUrl = "~/admin/private/cms/main/img/folderopen.gif";
			}

			tnc.Add( node );
		}
	}
	/// <summary>
	/// On demand population of data into a tree view node
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	void _tree_TreeNodePopulate(object sender, TreeNodeEventArgs e)
	{
		// Get the unique value 
		try
		{
			int iValue = Int32.Parse( e.Node.Value );
			BindTreeData( e.Node.ChildNodes, iValue );
		}
		catch{ return ; }
	}
	/// <summary>
	/// A node in the tree has been collapsed - track it and 
	/// change the image to show a closed node.
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	void _tree_TreeNodeCollapsed(object sender, TreeNodeEventArgs e)
	{
		e.Node.ImageUrl = "~/admin/private/cms/main/img/folderclosed.gif";
		try
		{
			int uv = Int32.Parse(e.Node.Value);
			RemoveExpand(uv);
		}
		catch { }
	}
	/// <summary>
	/// A node in the tree has been expanded - track it and
	/// change the image to show an open node.
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	void _tree_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
	{
		e.Node.ImageUrl = "~/admin/private/cms/main/img/folderopen.gif";
		try
		{
			int uv = Int32.Parse(e.Node.Value);
			AddExpand(uv);
		}
		catch { }
	}
	#endregion
	#region Expand / Collapse tracking
	/// <summary>
	/// Get the session containing the list of open node keys
	/// </summary>
	/// <returns></returns>
	private ArrayList GetSession()
	{
		ArrayList exList = (ArrayList)Session["content_ContentTree"];

		if (exList == null )
		{
			exList = new ArrayList();
			Session["content_ContentTree"] = exList;
		}
		return exList;
	}
	/// <summary>
	/// Adds the specified unique key value to the list of expanded keys
	/// </summary>
	/// <param name="uValue"></param>
	private void AddExpand(int uValue)
	{
		ArrayList l = GetSession();
		if (l.Contains(uValue)) return;
		l.Add(uValue);
	}
	/// <summary>
	/// Removes the specified unique key value from the list of expanded keys
	/// </summary>
	/// <param name="uValue"></param>
	private void RemoveExpand(int uValue)
	{
		ArrayList l = GetSession();
		l.Remove(uValue);
	}
	/// <summary>
	/// Determines if the specified unique key value is in the list of expanded keys
	/// </summary>
	/// <param name="uValue"></param>
	/// <returns></returns>
	private bool IsExpanded(int uValue)
	{
		ArrayList l = GetSession();
		if (l.Contains(uValue)) return true;
		return false;
	}
	#endregion
}
